package edu.westga.sketchit2009cs.players;

import java.util.ArrayList;
import java.util.List;

/**
 * PlayerHandler class handles the list of players in the game.
 * @author RJ Hill
 *
 */
public class PlayerHandler {

	private List<Player> playerList;
	
	public PlayerHandler() {
		playerList = new ArrayList<Player>();
	}
	
	/**
	 * Checks the players names for duplicate and adds a 
	 * number to the end if there is a duplicate.
	 * <p>
	 * Requires: thePlayer != null
	 * <p>
	 * Ensures: playerList.get(n).getName() != playerList.get(m).getName()
	 * @param thePlayer the player to check against the list
	 * @return returns the player with an updated name
	 */
	private Player checkPlayerName(Player thePlayer) {
		int count = findNumberOfDuplicatePlayerNames(thePlayer.getName());
		
		if(count > 0) {
			// player name is not good, add a number to it.
			String newName = thePlayer.getName() + count;
			thePlayer.setName(newName);
		}
		
		return thePlayer;
	}
	
	/**
	 * Adds a player to the list, first checking the 
	 * username against other names in the list.
	 * <p>
	 * Requires: thePlayer != null
	 * <p>
	 * Ensures: playerList.get(playerList.size()) == thePlayer
	 * @param thePlayer the player to add
	 */
	public void addPlayer(Player thePlayer) {
		playerList.add(checkPlayerName(thePlayer));
	}
	
	/**
	 * Removes a player from the list using their IP address
	 * which should be unique.
	 * <p>
	 * Requires: playerList.size() > 0
	 * <p>
	 * Ensures: playerList.size() == playerList.size()@pre - 1
	 * @param thePlayer the player to remove
	 */
	public void removePlayer(Player thePlayer) {
		int playerIndex = findPlayerByIPAddress(thePlayer.getIP());
		playerList.remove(playerIndex);
	}
	
	/**
	 * Returns the number of players in the list.
	 * @return number of players
	 */
	public int getPlayerCount() {
		return playerList.size();
	}
	
	/**
	 * Returns the player at the specified index.
	 * @param index the index of the player
	 * @return the player
	 */
	public Player getPlayer(int index) {
		return playerList.get(index);
	}
	
	/**
	 * Updates the drawing privileges for the players in the list.
	 * <p>
	 * Requires: playerList.size() > 0
	 * <p>
	 * Ensures: playerList.get(i).getDrawingPrivileges() == false
	 * 			&& playerList.get(i+1).getDrawingPrivileges() == true
	 */
	public void nextPlayerToDraw() {
		int i = findDrawingPlayer();
		
		playerList.get(i).setDrawingPriveleges(false);
		
		if(i == (playerList.size()-1)) {
			i = -1;
		}
		
		i++;
		playerList.get(i).setDrawingPriveleges(true);
	}
	
	/**
	 * Finds the player that is currently drawing.
	 * <p>
	 * Requires: nothing.
	 * <p>
	 * Ensures: playerList.get(i).getDrawingPrivileges() == true
	 * @return the index of the player that is currently drawing
	 */
	private int findDrawingPlayer() {
		for(int i = 0; i < playerList.size(); i++) {
			if(playerList.get(i).getDrawingPriveleges() == true) {
				return i;
			}
		}
		return -1;
	}
	
	/**
	 * Finds a player by their IP Address.
	 * <p>
	 * Requires: nothing.
	 * <p>
	 * Ensures: playerList.get(i).getIP() == theIPAddress
	 * @param theIPAddress the ip address of the player
	 * @return the index of the player with the specified ip address
	 */
	private int findPlayerByIPAddress(String theIPAddress) {
		for(int i = 0; i < playerList.size(); i++) {
			if(playerList.get(i).getIP() == theIPAddress) {
				return i;
			}
		}
		return -1;
	}
	
	/**
	 * Finds the number of players with duplicate names.
	 * <p>
	 * Requires: nothing.
	 * <p>
	 * Ensures: nothing.
	 * @param playerName the name of the player to find duplicates of
	 * @return the number of players with the same name
	 */
	private int findNumberOfDuplicatePlayerNames(String playerName) {
		int count = 0;
		for(int i = 0; i < playerList.size(); i++) {
			if(playerList.get(i).getOldName() == playerName) {
				count++;
			}
		}
		return count;
	}
	
}
